package com.jojo.store.controller;

import com.jojo.store.controller.ex.FileSizeException;
import com.jojo.store.controller.ex.FileStateException;
import com.jojo.store.controller.ex.FileTypeException;
import com.jojo.store.controller.ex.FileUploadIOException;
import com.jojo.store.service.ex.*;
import com.jojo.store.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;

/**
 * 表示控制层类的基类
 */
public class BaseController {
    /*操作成功的状态码 用final修饰，不可改变*/
    public static  final int OK=200;

@ExceptionHandler(ServiceException.class)
    public JsonResult<Void> handleException(Throwable e){
    JsonResult<Void> result = new JsonResult<>(e);
    if (e instanceof UsernameDuplicatedException){
            result.setState(4000);
            result.setMessage("用户名已被占用");

        }else if (e instanceof UnameNotFoundException){
        result.setState(5001);
        result.setMessage("用户数据不存在");
    }else if (e instanceof PwdNotMacthExcpetion){
        result.setState(5002);
        result.setMessage("用户名的密码错误");
    }else if (e instanceof InsertException){
        result.setState(5000);
        result.setMessage("注册时产生未知的异常");
    }else if (e instanceof updateException){
        result.setState(5003);
        result.setMessage("更新时产生未知的异常");
    }else if (e instanceof updateException){
        result.setState(5004);
        result.setMessage("要上传的信息无效：例如修改新旧密码相同，或你上传传了空值");
    }else if (e instanceof FileSizeException){
        result.setState(6001);
        result.setMessage("文件上传的大小出现异常");
    }else if (e instanceof FileTypeException){
        result.setState(6002);
        result.setMessage("文件上传的类型异常");
    }
    else if (e instanceof FileUploadIOException){
        result.setState(6003);
        result.setMessage("文件上传的写入出现异常");
    } else if (e instanceof FileSizeException){
        result.setState(6004);
        result.setMessage("文件上传为空值");
    }else if (e instanceof FileStateException){
        result.setState(6005);
        result.setMessage("文件状态异常");
    }else if (e instanceof AddressCountLimitException){
        result.setState(7001);
        result.setMessage("地址数量超过限制");
    }else if (e instanceof AccessDeniedException){
        result.setState(7002);
        result.setMessage("地址不属于用户");
    }else if (e instanceof AddressNotFontException){
        result.setState(7003);
        result.setMessage("地址找不到");
    }else if (e instanceof DeleteException){
        result.setState(7004);
        result.setMessage("删除数据异常");
    }else if (e instanceof DeleteException){
        result.setState(7005);
        result.setMessage("商品数据不存在");
    }
    return  result;
    }


    //将登录过的数据保存在session中 以便下次登录获取，优化服务系统
    //session存储在服务器中；与cookie相反
protected final Integer getUidBySession(HttpSession session){
   return Integer.valueOf(session.getAttribute("uid").toString());
}
    protected final String getUnmBySession(HttpSession session){
        return session.getAttribute("username").toString();
    }

}
